package com.fjwt.gz.core.utils.jasypt;/*
 *文件名: JasyptUtil
 *创建者: wangjie
 *创建时间:2023/3/7 19:18
 */

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @author wangjie
 * @version 1.0
 * @date 2023/3/7 19:18
 */

public class JasyptUtil {
    /**
     * 加密
     * @param password 配置文件中设定的加密盐值
     * @param value 加密值
     * @return 加密后的字符串
     */
    public static String encrypt(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return  encryptor.encrypt(value);
    }

    /**
     * 解密
     * @param password 配置文件中设定的加密盐值
     * @param value 解密密文
     * @return 解密后的字符串
     */
    public static String decrypt(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return encryptor.decrypt(value);
    }

    /**
     * 配置,对应yml中的配置
     * @param password 盐值
     * @return SimpleStringPBEConfig
     */
    public static SimpleStringPBEConfig cryptor(String password){
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        //设置盐值
        config.setPassword(password);
        //设置算法
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }


    public static void main(String[] args) {
        // 加密
        String encryptStr = encrypt("jasypt", "admin");
        // 解密
        String decryptStr = decrypt("jasypt", encryptStr);
        System.out.println("加密后:" + encryptStr);
        System.out.println("解密后:" + decryptStr);
    }
}
